package top.linyuxb.lycloud.gateway.starter.config;

import cn.hutool.extra.spring.EnableSpringUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.Filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.linyuxb.lycloud.common.properties.LyGatewayProperties;
import top.linyuxb.lycloud.gateway.starter.controller.LyGatewayClientController;
import top.linyuxb.lycloud.gateway.starter.utils.config.LyGatewayWebMvcConfigurerUtil;
import top.linyuxb.lycloud.gateway.starter.utils.filter.LyGatewayCorsFilterUtil;
import top.linyuxb.lycloud.gateway.starter.utils.interceptor.AadHandlerInterceptorUtil;
import top.linyuxb.lycloud.gateway.starter.utils.interceptor.TLogHandlerInterceptorUtil;

import java.util.List;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/5/31 16:51
 * @Desc: 网关配置
 * https://blog.csdn.net/yuechuzhixing/article/details/132141251
 * https://blog.csdn.net/u014131617/article/details/125917967
 * https://cloud.tencent.com/developer/article/1981326
 * https://gitee.com/dromara/TLog
 * https://www.cnblogs.com/antLaddie/p/14751540.html
 */
@Slf4j
@Configuration
@EnableSpringUtil
@EnableConfigurationProperties(LyGatewayProperties.class)
@Import(LyGatewayClientController.class)
@ConditionalOnProperty(
        prefix = "ly-gateway",
        name = "open",
        havingValue = "true"
)
public class LyGatewayConfig {
    @Resource
    private LyGatewayProperties lyRpcClientProperties;

    /**
     * 初始化跨域配置
     *
     * @return
     */
    @Bean(value = "lyGatewayCorsFilterRegistrationBean")
    @ConditionalOnProperty(
            prefix = "ly-gateway.cors-config",
            name = "open",
            havingValue = "true"
    )
    public FilterRegistrationBean<Filter> lyGatewayCorsFilterRegistrationBean() {
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(
                LyGatewayCorsFilterUtil.buildCorsFilter(lyRpcClientProperties.getCorsConfig())
        );
        bean.setOrder(0);
        return bean;
    }

    /**
     * 初始化aad登入配置
     *
     * @return
     */
    @Bean(value = "aadWebMvcConfigurer")
    @ConditionalOnProperty(
            prefix = "ly-gateway.aad",
            name = "open",
            havingValue = "true"
    )
    public HandlerInterceptor aadWebMvcConfigurer() {
        return AadHandlerInterceptorUtil.buildHandlerInterceptor(lyRpcClientProperties.getAad());
    }

    /**
     * 初始化tLog链路配置
     *
     * @return
     */
    @Bean(value = "tLogHandlerInterceptor")
    @ConditionalOnProperty(
            prefix = "ly-gateway.t-log",
            name = "open",
            havingValue = "true"
    )
    public HandlerInterceptor tLogHandlerInterceptor() {
        return TLogHandlerInterceptorUtil.buildHandlerInterceptor(lyRpcClientProperties.getTLog());
    }

    /**
     * aad配置、tLog配置生效
     *
     * @param handlerInterceptors
     * @param lyGatewayConverters
     * @return
     */
    @Bean(value = "lyGatewayWebMvcConfigurer")
    public WebMvcConfigurer lyGatewayWebMvcConfigurer(List<HandlerInterceptor> handlerInterceptors,
                                                      List<HttpMessageConverter<?>> lyGatewayConverters) {
        return LyGatewayWebMvcConfigurerUtil.buildWebMvcConfigurer(handlerInterceptors, lyGatewayConverters);
    }
}
